// import Vue from 'vue'
import axios from 'axios'
// import store from '@/store'
import {
  VueAxios
} from './axios'
import { Notification } from 'element-ui'
// import {
//   ACCESS_TOKEN
// } from '@/store/mutation-types'

// 创建 axios 实例
const service = axios.create({
  baseURL: process.env.VUE_APP_API_BASE_URL, // api base_url //process.env.VUE_APP_API_BASE_URL
  timeout: 60000 // 请求超时时间
})

const err = (error) => {
  if (error.response) {
    const data = error.response.data
    const token = ''
    if (error.response.status === 403) {
      Notification.error({
        title: 'Forbidden',
        message: '没有权限操作！'
        // message: data.message
      })
    }
    if (error.response.status === 401 && !(data.data && data.data.isLogin)) {
      Notification.error({
        title: 'Unauthorized',
        message: 'Authorization verification failed'
      })
      if (token) {
        // store.dispatch('Logout').then(() => {
        //   setTimeout(() => {
        //     window.location.reload()
        //   }, 1500)
        // })
      }
    }
  }
  return Promise.reject(error)
}

// request interceptor
service.interceptors.request.use(config => {
  // const token = Vue.ls.get(ACCESS_TOKEN)
  const token = ''
  if (token) {
    config.headers.Authorization = 'Bearer ' + token
    config.headers.jwt = token
  }
  return config
}, err)

// response interceptor
service.interceptors.response.use((response) => {
  return response.data
}, err)

const installer = {
  vm: {},
  install(Vue) {
    Vue.use(VueAxios, service)
  }
}

export {
  installer as VueAxios,
  service as axios
}
